adminApp.controller('memberMngCtrl', function($scope,$http,$state,$stateParams,memberService){
    $scope.search = {};
    $scope.search.pageIndex = 0;
    $scope.search.pageSize = 10;

    $scope.search.ascription = $("#ascription").val();
    $scope.ascription = $("#ascription").val() !== "";

    $scope.getPagedDataAsync = function(){
        $http.post("/uj-oa/member/list", $scope.search).success(function(data){

            $scope.list = data.list;
            $scope.count = data.count;
        });

    };

    $scope.pagedCallback = function (){
        $scope.search.pageIndex = $scope.pageIndex -1;
        $scope.getPagedDataAsync();
    };

    $scope.getPagedDataAsync();

    $scope.updateStatus = function(id){
        $http.put('/uj-oa/member/' + id).success(function(data){
            $scope.getPagedDataAsync();
        })
    }
});

adminApp.controller('memberFormCtrl',function($scope,$http,$state,$stateParams,memberService){

    $scope.ascription = $("#ascription").val() !== "";
    $scope.member = {};
    $scope.member.ascription = $("#ascription").val();

    if($stateParams.id > 0){
        memberService.getById($stateParams.id).success(function(data){
            $scope.member = data;
            $scope.memberLevel = "level" + $scope.member.level;
        })
    }

    $scope.save = function(){
        if($scope.member == null){
            $(".alert-danger").html("请输入电话");
            $(".alert-danger").show();
            $(".alert-danger").fadeOut(3000);
            return false
        }
        if($scope.member.real_name == null || $scope.member.real_name == ""){
            $(".alert-danger").html("请输入姓名");
            $(".alert-danger").show();
            $(".alert-danger").fadeOut(3000);
            return false;
        }
        if($scope.member.phone_mob == null || $scope.member.phone_mob == "" || !/^1[3-8]{1}\d{9}$/.test($scope.member.phone_mob)){
            $(".alert-danger").html("请输入电话");
            $(".alert-danger").show();
            $(".alert-danger").fadeOut(3000);
            return false;
        }
        if($scope.member.scores == null || $scope.member.scores == "" || !/^\d*$/.test($scope.member.scores)){
            $(".alert-danger").html("请输入正确的积分");
            $(".alert-danger").show();
            $(".alert-danger").fadeOut(3000);
            return false;
        }
        if($scope.member.ascription == null || $scope.member.ascription == ""){
            $(".alert-danger").html("请输入归属地");
            $(".alert-danger").show();
            $(".alert-danger").fadeOut(3000);
            return false;
        }

        $scope.member.user_name = $scope.member.phone_mob;

        if($stateParams.id > 0){
            $http.put("/uj-oa/member", $scope.member).success(function(data){
                $state.go("index.memberMng");
            }).error(function(data){
                $(".alert-danger").html(data.fieldErrors[0].message);
                $(".alert-danger").show();
                $(".alert-danger").fadeOut(3000);
            });
        }else{
            $http.post("/uj-oa/member", $scope.member).success(function(data){
                $state.go("index.memberMng");
            }).error(function(data){
                $(".alert-danger").html(data.fieldErrors[0].message);
                $(".alert-danger").show();
                $(".alert-danger").fadeOut(3000);
            });
        }
    }
});
